import { Sticky, NavBar, Overlay, Notify, Dialog } from "vant";
import PublicNullData from "@/components/PublicNullData/PublicNullData.vue";
import PublicError from "@/views/list/_components/deviceControl/Error/Error.vue";
import PageSkeleton from "@/views/list/_components/skeleton/skeleton.vue";
import OnOff from "@/views/Index/_components/onOff/onOff.vue";
import PageDeviceListDetail from "@/views/list/_components/deviceListDetail/deviceListDetail.vue";
import AcdMachine from "@/views/list/_components/deviceControl/AcdMachine/AcdMachine.vue";
import AdMachine from "@/views/list/_components/deviceControl/AdMachine/AdMachine.vue";
import AirCd from "@/views/list/_components/deviceControl/AirCd/AirCd.vue";
import AirController from "@/views/list/_components/deviceControl/AirController/AirController.vue";
import Co2 from "@/views/list/_components/deviceControl/Co2/Co2.vue";
import CurTain from "@/views/list/_components/deviceControl/CurTain/CurTain.vue";
import EdgeHost from "@/views/list/_components/deviceControl/EdgeHost/EdgeHost.vue";
import ElectricLock from "@/views/list/_components/deviceControl/ElectricLock/ElectricLock.vue";
import HcHo from "@/views/list/_components/deviceControl/HcHo/HcHo.vue";
import HumiDity from "@/views/list/_components/deviceControl/HumiDity/HumiDity.vue";
import IllMin from "@/views/list/_components/deviceControl/IllMin/IllMin.vue";
import InSenSor from "@/views/list/_components/deviceControl/InSenSor/InSenSor.vue";
import Ipb from "@/views/list/_components/deviceControl/Ipb/Ipb.vue";
import Light_Rgb from "@/views/list/_components/deviceControl/Light_Rgb/Light_Rgb.vue";
import LightBrt from "@/views/list/_components/deviceControl/LightBrt/LightBrt.vue";
import LightTemp from "@/views/list/_components/deviceControl/LightTemp/LightTemp.vue";
import M2AirSenSor from "@/views/list/_components/deviceControl/M2AirSenSor/M2AirSenSor.vue";
import M7AirSenSor from "@/views/list/_components/deviceControl/M7AirSenSor/M7AirSenSor.vue";
import Pm2p5 from "@/views/list/_components/deviceControl/Pm2p5/Pm2p5.vue";
import Pm10 from "@/views/list/_components/deviceControl/Pm10/Pm10.vue";
import RepeAter from "@/views/list/_components/deviceControl/RepeAter/RepeAter.vue";
import SmartSocket from "@/views/list/_components/deviceControl/SmartSocket/SmartSocket.vue";
import Smoke from "@/views/list/_components/deviceControl/Smoke/Smoke.vue";
import Spanel from "@/views/list/_components/deviceControl/Spanel/Spanel.vue";
import SwitchDevice from "@/views/list/_components/deviceControl/Switch/Switch.vue";
import TemPeraTure from "@/views/list/_components/deviceControl/TemPeraTure/TemPeraTure.vue";
import Voc from "@/views/list/_components/deviceControl/Voc/Voc.vue";
import FourKPING from "@/views/list/_components/deviceControl/4KPING/4KPING.vue";
import MobileEsocket from "@/views/list/_components/deviceControl/MobileEsocket/MobileEsocket.vue"
import {
  lot_area_list_api,
  lot_mark_query_api,
  lot_onOff_set_api,
  lot_equipmentControl_status_api,
} from "@/api/lot.js";
import { getLocalStorage } from "@/tools/index.js";
export default {
  name: "deviceList",
  components: {
    [Sticky.name]: Sticky,
    [NavBar.name]: NavBar,
    [Overlay.name]: Overlay,
    PublicNullData,
    PublicError,
    OnOff,
    PageSkeleton,
    PageDeviceListDetail,
    AcdMachine,
    AdMachine,
    AirCd,
    AirController,
    Co2,
    CurTain,
    EdgeHost,
    ElectricLock,
    HcHo,
    HumiDity,
    IllMin,
    InSenSor,
    Ipb,
    Light_Rgb,
    LightBrt,
    LightTemp,
    M2AirSenSor,
    M7AirSenSor,
    Pm2p5,
    Pm10,
    RepeAter,
    SmartSocket,
    Smoke,
    Spanel,
    SwitchDevice,
    TemPeraTure,
    Voc,
    FourKPING,
    MobileEsocket
  },
  data() {
    return {
      // 弹窗对象
      popupObj: {
        show: false,
        overlay: true,
      },
      // 列表对象
      listObj: {
        // 骨架
        isSkeleton: true,
        // 数据
        list: [],
        // 加载
        loading: false,
        // 加载完成
        finished: false,
      },
      // 设备对象
      equipmentObj: {
        // 设备类型
        specificationCode: "",
        // 设备Id
        equipmentId: "",
        // 设备名称
        equipmentName: "",
        // 设备状态
        onOffText: "",
        // 离在线状态
        equipmentStatusText: "",
        // 离在线状态类型
        equipmentStatusType: "",
        // 设备指令
        parameterArr: [],
      },
      // 查询入参
      queryCondition: {
        // 机构id
        orgId: getLocalStorage("loginInfo").orgId,
        // 设备id
        equipmentId: "",
        //区域id
        areaId: "",
        // 区域数组
        areaArr: [],
        //当前页
        page: 1,
        //每页条目数
        rows: 100000,
        // 类型
        cmd: "",
        // 设备类型id
        equipmentTypeId: "",
        // 设备命令
        equipmentCmd: [],
      },
    };
  },
  created() {},
  mounted() {
    // 区域列表查询
    this.lot_area_list_api();
    // 设置websocket
    // this.setWebsocket({
    //   websocketType: "onOff",
    //   orgId: this.queryCondition.orgId,
    // });
  },
  methods: {
    // 设置websocket
    setWebsocket(data) {
      let that = this;
      console.log(that.$global.ws);
      if (that.$global.ws && that.$global.ws.readyState == 1) {
        console.log("发送信息", JSON.stringify(data));
        that.$global.ws.send(JSON.stringify(data));
      }
      that.$global.ws.onmessage = function(res) {
        console.log("收到服务器内容", JSON.stringify(res));
        let obj = JSON.parse(res.data);
        console.info("obj.action::::" + obj.action);
        if (obj.action == "reflush") {
          that.lot_mark_query_api();
        }
      };
    },
    // 区域列表查询
    lot_area_list_api() {
      let data = {
        orgId: this.queryCondition.orgId,
      };
      console.info("区域列表查询入参：" + JSON.stringify(data));
      lot_area_list_api(data)
        .then((res) => {
          console.info("区域列表查询出参：" + JSON.stringify(res));
          if (res.status == 200) {
            this.queryCondition.areaArr = res.obj;
            this.queryCondition.areaId = res.obj[0].areaId;
            this.listObj.isSkeleton = true;
            this.lot_mark_query_api();
          } else {
            Notify({ type: "danger", message: res.msg });
          }
        })
        .catch((err) => {});
    },
    // 开关接口
    lot_onOff_set_api() {
      let data = {
        orgId: this.queryCondition.orgId,
        cmd: this.queryCondition.cmd,
        areaId: this.queryCondition.areaId,
        equipmentTypeId: this.queryCondition.equipmentTypeId,
        equipmentCmd: this.queryCondition.equipmentCmd,
      };
      console.info("lot_onOff_set_api开关接口入参" + JSON.stringify(data));
      lot_onOff_set_api(data)
        .then((res) => {
          console.info("lot_onOff_set_api开关接口出参" + JSON.stringify(res));
          if (res.status == 200) {
            Notify({ type: "success", message: "设备操作成功" });
          } else {
            Notify({ type: "danger", message: res.msg });
          }
        })
        .catch((err) => {
          Notify({ type: "danger", message: "设备操作失败" });
        });
    },
    // 设备控制分页查询
    lot_mark_query_api() {
      this.listObj.isSkeleton = true;
      let data = {
        relatedId: this.queryCondition.areaId,
        type: "3",
      };
      console.info(
        "lot_equipmentControl_page_api设备控制分页查询入参" +
          JSON.stringify(data)
      );
      lot_mark_query_api(data)
        .then((res) => {
          console.info(
            "lot_equipmentControl_page_api设备控制分页查询出参" +
              JSON.stringify(res)
          );
          if (res.status == 200) {
            setTimeout(() => {
              this.listObj.isSkeleton = false;
            }, 200);

            if (res.obj.elements) {
              this.listObj.list = res.obj.elements;
            } else {
              this.listObj.list = [];
            }
            // 加载状态结束
            this.listObj.loading = false;
            this.listObj.finished = true;
          } else {
            this.listObj.list = [];
            // 加载状态结束
            this.listObj.loading = false;
            this.listObj.finished = true;
            Notify({ type: "danger", message: res.msg });
          }
        })
        .catch((err) => {
          // 加载状态结束
          this.listObj.loading = false;
          this.listObj.finished = true;
          Notify({ type: "danger", message: "设备控制分页查询失败" });
        });
    },
    // 查询设备控制
    lot_equipmentControl_status_api(id, name) {
      this.equipmentObj.parameterArr = [];
      let data = {
        equipmentId: id,
      };
      console.info(
        "lot_equipmentControl_status_api查询设备控制入参" + JSON.stringify(data)
      );
      if (this.equipmentObj.isDisabled == false) {
        return;
      }
      this.equipmentObj.isDisabled = false;
      lot_equipmentControl_status_api(data)
        .then((res) => {
          console.info(
              "lot_equipmentControl_status_api查询设备控制出参" +
              JSON.stringify(res)
          );
          if (res.status == 200) {
            this.equipmentObj.isDisabled = true;
            // if (name) {
            //   Notify({ type: "success", message: "设备刷新成功" });
            // }
            this.equipmentObj.equipmentId = res.obj.equipmentId;
            this.equipmentObj.specificationCode =
              res.obj.specification.specificationCode;
            this.equipmentObj.fileName = res.obj.fileName;
            this.equipmentObj.equipmentName = res.obj.equipmentName;
            this.equipmentObj.onOffText = res.obj.onOff == 0 ? "停用" : "启用";
            this.equipmentObj.onOffType =
              res.obj.onOff == 0 ? "#ccc" : "#07c160";
            if (res.obj.equipmentStatus == 1) {
              this.equipmentObj.equipmentStatusText = "在线";
              this.equipmentObj.equipmentStatusType = "#07c160";
            } else if (res.obj.equipmentStatus == 2) {
              this.equipmentObj.equipmentStatusText = "离线";
              this.equipmentObj.equipmentStatusType = "#ccc";
            } else if (res.obj.equipmentStatus == 3) {
              this.equipmentObj.equipmentStatusText = "故障";
              this.equipmentObj.equipmentStatusType = "#ee0a24";
            }
            if (
              res.obj.specification != null &&
              res.obj.specification.status != null &&
              res.obj.specification.status.length > 0
            ) {
              res.obj.specification.status.forEach((item) => {
                let obj = JSON.parse(item.parameter);
                obj.forEach((objItem) => {
                  let parameterObj = {
                    parameterName: objItem.parameterName,
                    description: objItem.description,
                    defaultValue: objItem.defaultValue,
                    parameterType: objItem.parameterType,
                  };
                  this.equipmentObj.parameterArr.push(parameterObj);
                });
              });
            }
            this.random = Math.round(Math.random() * 10);

          } else {
            this.equipmentObj.isDisabled = true;
            Notify({ type: "danger", message: res.msg });
          }
        })
        .catch((err) => {
          this.equipmentObj.isDisabled = true;
          Notify({ type: "danger", message: "查询设备控制失败" });
        });
    },
    // 弹窗
    popupBtn(value) {
      this.equipmentObj.specificationCode = "ON_OFF";
      this.popupObj.show = value;
    },
    // 一键开监听
    onEmitFn() {
      Dialog.confirm({
        message: "是否确定区域一键开机",
      })
        .then(() => {
          this.queryCondition.cmd = "switchOn";
          this.queryCondition.areaId = this.queryCondition.areaId;
          // 开关接口
          this.lot_onOff_set_api();
        })
        .catch(() => {
          // on cancel
        });
    },
    // 一键关监听
    offEmitFn() {
      Dialog.confirm({
        message: "是否确定区域一键开机",
      })
        .then(() => {
          this.queryCondition.cmd = "switchOff";
          this.queryCondition.areaId = this.queryCondition.areaId;
          // 开关接口
          this.lot_onOff_set_api();
        })
        .catch(() => {
          // on cancel
        });
    },
    // 设备详情
    detailEmitFn(id) {
      this.lot_equipmentControl_status_api(id,'reLoaad');
      setTimeout(()=>{
        this.popupObj.show = true;
      },300)

    },
  },
};
